package com.tobeing.voicechat.activities;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.tobeing.voicechat.R;
import com.tobeing.voicechat.adapters.RecorderAdapter;
import com.tobeing.voicechat.customs.RecorderPlay;
import com.tobeing.voicechat.models.Recorder;
import com.tobeing.voicechat.views.RecorderButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView mLVRecorder;
    private RecorderButton mBtnRecorder;

    private RecorderAdapter mAdapter;
    private List<Recorder> mRecorders = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initData();
        initEvents();
    }

    @Override
    protected void onResume() {
        super.onResume();
        RecorderPlay.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        RecorderPlay.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RecorderPlay.release();
    }

    private void initViews(){
        mLVRecorder = findViewById(R.id.lv_recorder);
        mBtnRecorder = findViewById(R.id.btn_recorder);
    }

    private void initData() {
        mAdapter = new RecorderAdapter(this, mRecorders);
        mLVRecorder.setAdapter(mAdapter);
    }

    private void initEvents(){
        mBtnRecorder.setRecorderFinishListener(new RecorderButton.RecorderFinishListener() {
            @Override
            public void finished(String path, float time) {
                Recorder recorder = new Recorder(time, path);
                mRecorders.add(recorder);
                mAdapter.notifyDataSetChanged();
                mLVRecorder.setSelection(mRecorders.size() - 1);
            }
        });
    }
}
